<?php
/**
 * 打折管理
 * @author Ice iceinto@mallog.com.cn
 */
class DiscountModel extends BaseModel
{
    /**
     * 获取资讯详细
     * @param Array $baseData 一个资讯信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function getDisDetail (&$baseData)
    {
        $MarketModel = M('Market');
        $FloorModel = M('Floor');
        $BrandModel = M('Brand');
        $GoodModel = M('Good');
        $map = array();
        foreach ($baseData as $k => $b) {
            //商场信息
            if ($b['mid']) {
                unset($map);
                $map['id'] = $b['mid'];
                $rs = $MarketModel->where($map)
                    ->field('name')
                    ->find();
                $baseData[$k]['m_name'] = $rs['name'];
            }
            //楼层信息
            if ($b['fid']) {
                unset($map);
                $map['id'] = $b['fid'];
                $rs = $FloorModel->where($map)
                    ->field('sain,name')
                    ->find();
                $baseData[$k]['f_sain'] = $rs['sain'];
                $baseData[$k]['f_name'] = $rs['name'];
            }
            //品牌信息
            if ($b['bid']) {
                unset($map);
                $map['id'] = $b['bid'];
                $rs = $BrandModel->where($map)
                    ->field('name')
                    ->find();
                $baseData[$k]['b_name'] = $rs['name'];
            }
            //商品信息
            if ($b['gid']) {
                unset($map);
                $map['id'] = $b['gid'];
                $rs = $GoodModel->where($map)
                    ->field('name')
                    ->find();
                $baseData[$k]['g_name'] = $rs['name'];
            }
        }
    }
    /**
     * 获取所有打折信息存在分页
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAllPor ()
    {
        $count = $this->count();
        $Page = $this->getPage($count);
        $baseData = $this->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('etime ASC')
            ->select();
        $this->getDisDetail(&$baseData);
        return array('page' => $Page->show(), 'data' => $baseData);
    }
    /**
     * 打折信息处理添加或修改
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function disUpdate ($data)
    {
        $map = array();
        $this->status['static'] = 0;
        //商场活动
        $map['mid'] = $data['mid'];
        $map['fid'] = 0;
        $map['bid'] = 0;
        $map['gid'] = 0;
        //品牌活动
        if ($data['fid'] && $data['bid']) {
            $map['fid'] = $data['fid'];
            $map['bid'] = $data['bid'];
        }else{
        	$map['gid'] = 0;
        }
        //商品活动
        if ($data['fid'] && $data['bid'] && $data['gid']) {
            $map['fid'] = $data['fid'];
            $map['bid'] = $data['bid'];
            $map['gid'] = $data['gid'];
        }
        //执行验证
        $this->checkPostData($data);
        //日期转换
        $data['stime'] = strtotime($data['stime']);
        $data['etime'] = strtotime($data['etime']);
        if ($data['stime'] > $data['etime']) {
            $this->status['static'] = 0;
            $this->status['doc'] = '结束时间不能小于开始时间！';
            return $this->status;
        }
        $dis = $this->where($map)->find();
        //TODO 判断日期大小情况
        if ($dis) {
            //修改 strtotime($data['etime'])
            $data['id'] = $dis['id'];
            $this->status['status'] = 1;
            $this->BaseUpdate($data);
        } else {
            //添加
            $this->status['status'] = 1;
            $this->BaseAdd($data);
        }
        return $this->status;
    }
    /**
     * 进行数据检测
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function checkPostData ($data)
    {
        $marketModel = M('Market');
        $map = array();
        $map['id'] = $data['mid'];
        $market = $marketModel->where($map)->find();
        if ($market) {
        	return true;
        }
        return false;
    }
    /**
     * 获取已经过去的资讯
     * @author Ice iceinto@mallog.com.cn
     */
    public function getExpired ()
    {
        $map = array();
        $map['etime'] = array('lt', time());
        $data = $this->limit('0,10')
            ->order('etime ASC')
            ->select();
        $this->getDisDetail(&$data);
        return $data;
    }
}
?>